﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace studentskicentarETFa
{
    partial class DAL
    {
        public class StudentDAO : IDaoCrud<Student>
        {
            protected MySqlCommand komanda;
            public long create(Student entitet)
            {
                try
                {
                    komanda = new MySqlCommand();
                    komanda.CommandText = "insert into Student values (@ID_Clana, @ID_Kartice,@Ime,@Prezime,@BrojIndexa,@BrojTelefona)";
                    komanda.Parameters.AddWithValue("@ID_Clana", entitet.ID_Clana1);
                    komanda.Parameters.AddWithValue("@ID_Kartice", entitet.ID_Kartice1);
                    komanda.Parameters.AddWithValue("@Ime", entitet.Ime);
                    komanda.Parameters.AddWithValue("@Prezime", entitet.Prezime);
                    komanda.Parameters.AddWithValue("@BrojIndexa", entitet.BrojIndexa);
                    komanda.Parameters.AddWithValue("@BrojTelefona", entitet.BrojTelefona);
                    //komanda.Parameters.AddWithValue("@DatmRegistracije", entitet.Datum_registracije);
                    komanda.Connection = con;
                    komanda.Prepare();
                    komanda.ExecuteNonQuery();
                    con.Close();
                    return 1;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
           
            public Student read(Student entitet)
            {
                try
                {
                    komanda = new MySqlCommand("select * from Student where ID_Clana = '" + entitet.ID_Clana1.ToString() + "';", con);
                    MySqlDataReader citac = komanda.ExecuteReader();
                    Student student = null;
                    while (citac.Read())
                    {
                        //if (entitet.Ime == citac.GetString("ime") && entitet.Prezime == citac.GetString("prezime"))
                        student = new Student(citac.GetString("Ime"), citac.GetString("Prezime"));
                        return student;
                    }
                    citac.Close();
                    return student;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            public Student update(Student entitet)
            {
                try
                {
                    komanda.CommandText = "update Student set ID_Clana=@ID_Clana1, ID_Kartice=@ID_Kartice1,Ime=@Ime,Prezime=@Prezime,BrojIndexa=@BrojIndexa,BrojTelefona=@BrojTelefona";
                    komanda.Parameters.AddWithValue("@ID_Clana", entitet.ID_Clana1);
                    komanda.Parameters.AddWithValue("@ID_Kartice", entitet.ID_Kartice1);
                    komanda.Parameters.AddWithValue("@Ime", entitet.ID_Clana1);
                    komanda.Parameters.AddWithValue("@Prezime", entitet.ID_Clana1);
                    komanda.Parameters.AddWithValue("@BrojIndexa", entitet.ID_Clana1);
                    komanda.Parameters.AddWithValue("@BrojTelefona", entitet.ID_Clana1);
                   // komanda.Parameters.AddWithValue("@DatumRegistracije", entitet.ID_Clana1);
                    komanda.Connection = con;
                    komanda.Prepare();
                    komanda.ExecuteNonQuery();
                    con.Close();
                    return read(entitet);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            
            public void delete(Student entitet)
            {
                try
                {
                    komanda = new MySqlCommand("delete from Student where ID_Clana = " + entitet.ID_Clana1 + ";", con);
                    komanda.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            public List<Student> getAll()
            {
                try
                {
                    komanda = new MySqlCommand("select * from Student", con);
                    MySqlDataReader r = komanda.ExecuteReader();
                    List<Student> studenti = new List<Student>();
                    while (r.Read())
                        studenti.Add(new Student(r.GetString("ID_Clana"), r.GetString("ID_Kartice"), r.GetString("Ime"), r.GetString("Prezime"), r.GetInt32("BrojIndexa"), r.GetString("BrojTelefona")/*, r.GetDateTime("DatumRegistracije")*/));
                    r.Close();
                    return studenti;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }


            public List<Student> getByExample(string ime, string prezime)
            {
                try
                {
                    MySqlCommand n = new MySqlCommand("se1ect ‘from Studenti where '" + prezime + "'='" + ime + "'", con);
                    List<Student> studenti = new List<Student>();
                    MySqlDataReader r = n.ExecuteReader();
                    while (r.Read())
                    {
                        studenti.Add(new Student(r.GetString("Ime"),r.GetString("Prezime")));
                    }
                    r.Close();
                    return studenti;
                }
                catch (Exception)
                {
                    throw;
                }

            }

            public Student getById(int id)
            {
                try
                {
                    MySqlCommand n = new MySqlCommand("se1ect * from Student where ID_Clana='" + id + "", con);
                    Student s = null;
                    MySqlDataReader r = n.ExecuteReader();
                    while (r.Read())
                    {
                        s = new Student(r.GetString("ID_Clana"), r.GetString("ID_Kartice"), r.GetString("Ime"), r.GetString("Prezime"), r.GetInt32("BrojIndexa"), r.GetString("BrojTelefona")/*, r.GetDateTime("DatumRegistracije")*/);
                        
                    }
                    r.Close();
                    return s;
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }
    }
}
    
